package com.jiutong.meipengdai.ui.activitys;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bm.library.Info;
import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.EvaluateListBean;
import com.jiutong.meipengdai.entity.EvaluateRespond;
import com.jiutong.meipengdai.http.model.interfaces.LoanRelaBize;
import com.jiutong.meipengdai.http.model.requests.ImageUploadRequest;
import com.jiutong.meipengdai.http.model.requests.LoanRelaRequest;
import com.jiutong.meipengdai.http.progress.ProgressDialogHandler;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.DensityUtil;
import com.jiutong.meipengdai.utils.ImageUtil;
import com.jiutong.meipengdai.utils.PermissionUtil;
import com.jiutong.meipengdai.utils.UIUtils;
import com.jiutong.meipengdai.views.dialog.BaseDialog;
import com.jiutong.meipengdai.views.dialog.DiractionPickerDialog;
import com.jiutong.meipengdai.views.dialog.DoNetWorkCityPicker;
import com.jiutong.meipengdai.views.dialog.TimePickerDialog;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 额度评估界面
 * Created by suxi on 2017/5/24.
 */

public class EvaluateActivity extends BaseActivity implements LoanRelaBize {

    public final static int XIAO_QU = 1;
    private final int REQUEST_ALBUM = 0;
    private final String IMAGE_TYPE = "image/*";
    private final int REQUEST_CAMERA = 1;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_city)
    TextView tvCity;
    @Bind(R.id.rl_city_select)
    RelativeLayout rlCitySelect;
    @Bind(R.id.et_area)
    EditText etArea;
    @Bind(R.id.et_xiaoqu)
    TextView et_xiaoqu;
    @Bind(R.id.tv_chaoxiang)
    TextView tvChaoxiang;
    @Bind(R.id.rl_chaoxiang)
    RelativeLayout rlChaoxiang;
    @Bind(R.id.et_level)
    EditText etLevel;
    @Bind(R.id.et_total_floor)
    EditText etTotalFlot;
    @Bind(R.id.btn_submit)
    Button btnSubmit;
    @Bind(R.id.rl_show_pic)
    RelativeLayout rl_show_pic;
    @Bind(R.id.ll_show_pic)
    RelativeLayout ll_show_pic;
    @Bind(R.id.iv_house_img)
    PhotoView iv_house_img;
    @Bind(R.id.tv_keep_fill)
    TextView tvKeepFill;
    @Bind(R.id.tv_shouqi)
    TextView tvShouqi;
    @Bind(R.id.et_house_id)
    EditText etHouseId;
    @Bind(R.id.et_belong)
    EditText etBelong;
    @Bind(R.id.et_id_card)
    EditText etIdCard;
    @Bind(R.id.tv_start_time)
    TextView tvStartTime;
    @Bind(R.id.tv_end_time)
    TextView tvEndTime;
    @Bind(R.id.tv_register_time)
    TextView tvRegisterTime;
    @Bind(R.id.et_register_price)
    EditText etRegisterPrice;
    @Bind(R.id.et_land_num)
    EditText etLandNum;
    @Bind(R.id.et_land_area)
    EditText etLandArea;
    @Bind(R.id.ll_more_content)
    LinearLayout llMoreContent;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.et_building_num)
    EditText etBuildingNum;
    @Bind(R.id.et_room_num)
    EditText etRoomNum;
    @Bind(R.id.iv_turn_big)
    ImageView ivTurnBig;
    @Bind(R.id.fl_bg)
    FrameLayout fl_bg;
    @Bind(R.id.black_color_bg)
    ImageView ivBlackBg;
    @Bind(R.id.big_img)
    PhotoView bigImg;
    @Bind(R.id.tv_img_des)
    TextView tvImageDes;
    @Bind(R.id.tv_update_again)
    TextView tvupdateAgain;
    @Bind(R.id.rl_comment_title)
    RelativeLayout rl_comment_title;
    @Bind(R.id.rl_evaluate_xiaoqu)
    RelativeLayout rlEvaluateXiaoqu;
    @Bind(R.id.rl_area)
    RelativeLayout rlArea;


    AlphaAnimation animationIn = new AlphaAnimation(0, 1);
    AlphaAnimation animationOut = new AlphaAnimation(1, 0);
    private List<EvaluateListBean> list;
    private BaseDialog dialog;
    private String imgPath;
    private DiractionPickerDialog diractionPickerDialog;
    private TimePickerDialog startTimePickerDialog;
    private TimePickerDialog endTimePickerDialog;
    private TimePickerDialog registerTimePickerDialog;
    private LoanRelaRequest loanRelaRequest;
    private SubscriberOnNextListener onNextListener;
    private HashMap<String, Object> fieldsMap;
    private String strByBase64;
    private String startTime = "";
    private String endTime = "";
    private String registerTime = "";
    private String mDiraction = "";
    private int mStartYear;
    private int mStartMonth;
    private DoNetWorkCityPicker citySelectedDialog;
    private String cityCode;
    private Info mInfo;
    private ProgressDialog progressDialog;
    private InputMethodManager imManager;

    /*
* 判断sdcard是否被挂载
*/
    public static boolean hasSdcard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    public String getPath() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "";
    }

    public String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        Bundle extras = getIntent().getExtras();

        if (savedInstanceState != null) {
            imgPath = savedInstanceState.getString("picpath");
        }

        if (extras != null) {
            imgPath = extras.getString("picpath");
        }

    }

    @Override
    protected void initView() {
        super.initView();

        ButterKnife.bind(this);

        //手动计算状态栏高度，否则查看图片你的时候，图片的位置信息有偏差，偏差恰好是状态栏高度
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl_comment_title.getLayoutParams();
        params.topMargin = DensityUtil.getTitleBarHeight(mContext);//获取状态蓝高度
        rl_comment_title.setLayoutParams(params);

        tvTitle.setText("房产评估");
        tvCity.setFocusable(true);
        tvCity.setFocusableInTouchMode(true);
        tvCity.requestFocus();

        if (!TextUtils.isEmpty(imgPath)) {
            handlePicPathToShow();
            tvImageDes.setVisibility(View.GONE);
            ivTurnBig.setVisibility(View.VISIBLE);
            tvupdateAgain.setVisibility(View.VISIBLE);
        }

        iv_house_img.disenable();
        bigImg.enable();

        bigImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        animationIn.setDuration(300);
        animationOut.setDuration(300);
        animationOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ivBlackBg.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("picpath", imgPath);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            imgPath = savedInstanceState.getString("picpath");
        }
        super.onRestoreInstanceState(savedInstanceState);
    }

    /**
     * 处理图片路径，并显示出来
     */
    private void handlePicPathToShow() {

        Glide.with(mContext).load(new File(imgPath)).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                iv_house_img.setImageBitmap(resource);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap = ImageUtil.ratio(resource, 800, 480);
                        strByBase64 = ImageUtil.Bitmap2StrByBase64(resource);
                    }
                }).start();
            }
        });

        Glide.with(mContext).load(imgPath).into(bigImg);
    }

    @OnClick({R.id.rl_back, R.id.rl_city_select, R.id.rl_show_pic, R.id.rl_evaluate_xiaoqu, R.id.rl_area,
            R.id.btn_submit, R.id.tv_keep_fill, R.id.tv_shouqi, R.id.tv_chaoxiang, R.id.tv_img_des, R.id.tv_update_again,
            R.id.tv_start_time, R.id.tv_end_time, R.id.tv_register_time, R.id.et_xiaoqu, R.id.iv_turn_big, R.id.big_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back://
                finish();
                break;
            case R.id.rl_city_select://城市
                cityList();
                break;
            case R.id.rl_show_pic://拍照
//                showTakePhotoDialog();
                break;
            case R.id.tv_chaoxiang://朝向
                showDiractionPickerDialog();
                break;
            case R.id.btn_submit://

                if (TextUtils.isEmpty(getAreaName())) {
                    toastShortShow("小区名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(getArea())) {
                    toastShortShow("面积不能为空");
                    return;
                }
                if (TextUtils.isEmpty(getInFloor())) {
                    toastShortShow("请输入第几层");
                    return;
                }
                if (TextUtils.isEmpty(getTotalFloor())) {
                    toastShortShow("请输入总楼层");
                    return;
                }
                if (TextUtils.isEmpty(getBuildingNum())) {
                    toastShortShow("请输入楼栋号");
                    return;
                }
                if (TextUtils.isEmpty(getRoomNum())) {
                    toastShortShow("请输入门牌号");
                    return;
                }

                showUploadProgressDialog();
                if (strByBase64 == null) {
                    autoAuth();
                } else {
                    imageUpload();
                }
                break;
            case R.id.tv_shouqi:    //收起
                llMoreContent.setVisibility(View.GONE);
                tvKeepFill.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_keep_fill:    //展开
                llMoreContent.setVisibility(View.VISIBLE);
                tvKeepFill.setVisibility(View.GONE);
                break;

            case R.id.tv_start_time:  //开始时间
                showStartTimeDialog();
                break;
            case R.id.tv_end_time:  //有效时间
                showEndTimeDialog();
                break;
            case R.id.tv_register_time:  //登记日期
                showRegisterTimeDialog();
                break;
            case R.id.iv_turn_big:

                mInfo = iv_house_img.getInfo();

//                mPhotoView.setImageResource(imgs[position]);
                ivBlackBg.startAnimation(animationIn);
                ivBlackBg.setVisibility(View.VISIBLE);
                fl_bg.setVisibility(View.VISIBLE);

                bigImg.animaFrom(mInfo);
                break;
            case R.id.big_img:
                ivBlackBg.startAnimation(animationOut);
                bigImg.animaTo(mInfo, new Runnable() {
                    @Override
                    public void run() {
                        fl_bg.setVisibility(View.GONE);
                    }
                });
                break;
            case R.id.tv_img_des:
            case R.id.tv_update_again:
                showTakePhotoDialog();
                break;

            case R.id.rl_evaluate_xiaoqu:
                et_xiaoqu.requestFocus();

                getIMManager().showSoftInput(et_xiaoqu, 0);
                break;
            case R.id.rl_area:
                etArea.requestFocus();

                getIMManager().showSoftInput(etArea, 0);
                break;
        }
    }

    private InputMethodManager getIMManager() {
        if (imManager == null)
            imManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        return imManager;
    }

    private void showUploadProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在评估请稍后");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    private void dismissUploadProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    /**
     * 显示登记时间选择器
     */
    private void showRegisterTimeDialog() {
        if (registerTimePickerDialog == null) {
            registerTimePickerDialog = new TimePickerDialog();
            registerTimePickerDialog.setOnDialogClickListener(new TimePickerDialog.OnDialogClickListener() {
                @Override
                public void onBtnClick(String year, String month, String data) {
                    if (Integer.parseInt(month) < 10) {
                        month = "0" + month;
                    }
                    if (Integer.parseInt(data) < 10) {
                        data = "0" + data;
                    }

                    registerTime = year + "-" + month + "-" + data;
                    tvRegisterTime.setText(registerTime);
                    registerTimePickerDialog.dismiss();
//                    tvRegisterTime.setText(year + "年" + month + "月" + data + "日");
                }
            });
        }

        registerTimePickerDialog.show(getSupportFragmentManager(), TAG);
    }

    private void showEndTimeDialog() {
        if (endTimePickerDialog == null) {
            endTimePickerDialog = new TimePickerDialog();

            endTimePickerDialog.setStartYear(1947);
            endTimePickerDialog.setOnDialogClickListener(new TimePickerDialog.OnDialogClickListener() {
                @Override
                public void onBtnClick(String year, String month, String data) {
                    if (Integer.parseInt(month) < 10) {
                        month = "0" + month;
                    }
                    if (Integer.parseInt(data) < 10) {
                        data = "0" + data;
                    }


                    if (Integer.parseInt(year) <= mStartYear) {
                        toastShortShow("必须大于开始时间");
                        return;
                    }

                    endTime = year + "-" + month + "-" + data;
                    tvEndTime.setText(endTime);
//                    tvEndTime.setText(year + "年" + month + "月" + data + "日");
                    endTimePickerDialog.dismiss();
                }
            });

            endTimePickerDialog.setEndYear(2100);
            endTimePickerDialog.setCurrentYear(2017);
        }

        endTimePickerDialog.show(getSupportFragmentManager(), TAG);
    }

    private void showStartTimeDialog() {
        if (startTimePickerDialog == null) {
            startTimePickerDialog = new TimePickerDialog();
            startTimePickerDialog.setOnDialogClickListener(new TimePickerDialog.OnDialogClickListener() {

                private int mStartData;

                @Override
                public void onBtnClick(String year, String month, String data) {

                    mStartYear = Integer.parseInt(year);
                    mStartMonth = Integer.parseInt(month);
                    mStartData = Integer.parseInt(data);
                    if (Integer.parseInt(month) < 10) {
                        month = "0" + month;
                    }
                    if (Integer.parseInt(data) < 10) {
                        data = "0" + data;
                    }

                    startTime = year + "-" + month + "-" + data;
                    tvStartTime.setText(startTime);
                    startTimePickerDialog.dismiss();
//                    tvStartTime.setText(year + "年" + month + "月" + data + "日");
                }
            });
            startTimePickerDialog.setStartYear(1947);
            startTimePickerDialog.setEndYear(2100);
            startTimePickerDialog.setCurrentYear(2017);
        }

        startTimePickerDialog.show(getSupportFragmentManager(), TAG);
    }

    private void showDiractionPickerDialog() {

        if (diractionPickerDialog == null) {
            diractionPickerDialog = new DiractionPickerDialog();

            diractionPickerDialog.setOnDialogClickListener(new DiractionPickerDialog.OnDialogClickListener() {

                @Override
                public void onBtnClick(String diraction) {
                    tvChaoxiang.setText(diraction);
                    mDiraction = diraction;
                    tvChaoxiang.setTextColor(getResources().getColor(R.color.loan_desc));
                }
            });
        }

        diractionPickerDialog.show(getSupportFragmentManager(), TAG);
    }

    private void showTakePhotoDialog() {
        if (dialog == null) {

            dialog = new BaseDialog.Builder()
                    .layResuorce(R.layout.activity_user_info_head_dialog)
                    .okViewClickEvent(R.id.tv_dialog_take_photo)
                    .ok2ViewClickEvent(R.id.tv_dialog_phone)
                    .cancelViewClickEvent(R.id.tv_dialog_cancel)
                    .outSideCancel(true)
                    .build();

            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                    //拍照
                    openCamera();
                    dialog.dismiss();
                }

                @Override
                public void onOk2Click() {
                    //相册
                    openAlbum();
                    dialog.dismiss();
                }
            });
        }

        dialog.show(getSupportFragmentManager(), TAG);
    }

    private void openCamera() {

        //检查系统是否开启了拍照位置权限;检查系统是否开启了读写权限;
        if (!PermissionUtil.checkSelfPermission(this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            return;
        }

        if (hasSdcard()) {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); // 格式化时间
            String filename = format.format(date) + ".jpg";
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机

            File fileFolder = new File(Environment.getExternalStorageDirectory()
                    + "/jiutong/");
            if (!fileFolder.exists()) { // 如果目录不存在，则创建一个名为"jiutong"的目录
                fileFolder.mkdir();
            }

            File jpgFile = new File(fileFolder, filename);
            imgPath = jpgFile.getPath();
            int currentapiVersion = Build.VERSION.SDK_INT;
            if (currentapiVersion < 24) {
                // 从文件中创建uri
                Uri uri = Uri.fromFile(jpgFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            } else {
                //兼容android7.0 使用共享文件的形式
                ContentValues contentValues = new ContentValues(1);
                contentValues.put(MediaStore.Images.Media.DATA, jpgFile.getAbsolutePath());
                Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            }
//            Uri photoUri = Uri.fromFile(jpgFile); // 传递路径
//            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系统默认存储路径

            startActivityForResult(intent, REQUEST_CAMERA);
        }
    }

    private void openAlbum() {

        boolean isKitKatO = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        Intent getAlbum;
        if (isKitKatO) {
            getAlbum = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        } else {
            getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
        }
        getAlbum.setType(IMAGE_TYPE);

        startActivityForResult(getAlbum, REQUEST_ALBUM);
    }

    private void autoAuth() {

        if (Integer.parseInt(getTotalFloor()) < Integer.parseInt(getInFloor())) {
            toastShortShow("总楼层必须大于所选楼层");
            return;
        }

        if (loanRelaRequest == null) {
            loanRelaRequest = new LoanRelaRequest();
        }
        onNextListener = new SubscriberOnNextListener<EvaluateRespond>() {
            @Override
            public void onNext(EvaluateRespond subjects) {

                dismissUploadProgressDialog();
                if (!"200".equals(subjects.getResCode())) {
                    toastShortShow(subjects.getResDesc());
                    return;
                }

                Bundle bundle = new Bundle();
                bundle.putString("houseId", ((EvaluateRespond) subjects).getData().getOlAssessmentId());
                openActivity(EvaluateResultActivity.class, bundle);
                dismissUploadProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                dismissUploadProgressDialog();
            }
        };

        fieldsMap = new HashMap<>();

        fieldsMap.put("areaName", getAreaName());
        fieldsMap.put("localName", getLocaName());
        fieldsMap.put("orientate", getOrientate());
        fieldsMap.put("area", getArea());
        fieldsMap.put("inFloor", getInFloor());
        fieldsMap.put("totalFloor", getTotalFloor());
        fieldsMap.put("houseCardFull", getHouseNum());
        fieldsMap.put("obligee", getBelong());
        fieldsMap.put("idCardNo", getIdCard());
        fieldsMap.put("serviceLifeStart", getStartTime());
        fieldsMap.put("serviceLifeEnd", getEndTime());
        fieldsMap.put("registerDate", getRegisterTime());
        fieldsMap.put("registerPrice", getRegisterPrice());
        fieldsMap.put("parcelNo", getLandNum());
        fieldsMap.put("parcelArea", getLandArea());
        fieldsMap.put("buildNo", getBuildingNum());
        fieldsMap.put("roomNo", getRoomNum());

        loanRelaRequest.autoAuth(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    //城市列表选择器
    private void cityList() {
        if (citySelectedDialog == null) {
            citySelectedDialog = new DoNetWorkCityPicker();
            citySelectedDialog.setOnDialogClickListener(new DoNetWorkCityPicker.OnDialogClickListener() {


                @Override
                public void onBtnClick(String city, String area, String code) {
                    tvCity.setText(city + area);
                    cityCode = code;
                }
            });
        }
        citySelectedDialog.show(getSupportFragmentManager(), TAG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) { // 如果返回数据
            if (requestCode == REQUEST_CAMERA) { // 拍照数据
                handlePicPathToShow();
            } else if (requestCode == REQUEST_ALBUM) {//相册
                // 从相册返回的数据
                if (data != null) {
                    final Uri uri = data.getData();

//                    imgPath = uri;
                    Glide.with(mContext).load(uri).asBitmap().into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            iv_house_img.setImageBitmap(resource);

                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    Bitmap bitmap = ImageUtil.ratio(resource, 800, 480);
                                    strByBase64 = ImageUtil.Bitmap2StrByBase64(bitmap);
                                }
                            }).start();
                        }
                    });

                    Glide.with(mContext).load(uri).into(bigImg);
                }
            }

            tvImageDes.setVisibility(View.GONE);
            ivTurnBig.setVisibility(View.VISIBLE);
            tvupdateAgain.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 小区
     *
     * @return
     */
    @Override
    public String getAreaName() {
        return et_xiaoqu.getText().toString().trim();
    }

    /**
     * 所在城市
     *
     * @return
     */
    @Override
    public String getLocaName() {
        return cityCode;
    }

    /**
     * @return 朝向
     */
    @Override
    public String getOrientate() {
        return mDiraction;
    }

    /**
     * @return 面积
     */
    @Override
    public String getArea() {
        return etArea.getText().toString().trim();
    }

    /**
     * @return 楼栋号
     */
    public String getBuildingNum() {
        return etBuildingNum.getText().toString().trim();
    }

    /**
     * @return 门牌号
     */
    public String getRoomNum() {
        return etRoomNum.getText().toString().trim();
    }

    /**
     * @return 楼层
     */
    @Override
    public String getInFloor() {
        return etLevel.getText().toString().trim();
    }

    /**
     * @return 总楼层
     */
    @Override
    public String getTotalFloor() {
        return etTotalFlot.getText().toString().trim();
    }

    /**
     * @return 房产证号
     */
    @Override
    public String getHouseNum() {
        return etHouseId.getText().toString().trim();
    }

    /**
     * @return 权利人
     */
    public String getBelong() {
        return etBelong.getText().toString().trim();
    }

    /**
     * @return 身份证号
     */
    public String getIdCard() {
        return etIdCard.getText().toString().trim();
    }

    /**
     * @return 开始时间
     */
    public String getStartTime() {
        return startTime;
    }

    /**
     * @return 结束时间
     */
    public String getEndTime() {
        return endTime;
    }

    /**
     * @return 登记时间
     */
    public String getRegisterTime() {
        return registerTime;
    }

    /**
     * @return 登记价格
     */
    public String getRegisterPrice() {
        return etRegisterPrice.getText().toString().trim();
    }

    /**
     * @return 宗地号
     */
    public String getLandNum() {
        return etLandNum.getText().toString().trim();
    }

    /**
     * @return 宗地面积
     */
    public String getLandArea() {
        return etLandArea.getText().toString().trim();
    }


    @Override
    public void onSuccess(Object object) {

    }

    /**
     * 图片上传成功，才开始评估
     */
    private void imageUpload() {

        if (Integer.parseInt(getTotalFloor()) < Integer.parseInt(getInFloor())) {
            toastShortShow("总楼层必须大于所选楼层");
            return;
        }

        SubscriberOnNextListener<BaseRespond> nextListener = new SubscriberOnNextListener<BaseRespond>() {

            @Override
            public void onNext(BaseRespond respond) {
                Logger.e(respond.getResDesc());
                if ("200".equals(respond.getResCode())) {
                    autoAuth();
                } else {
                    toastShortShow(respond.getResDesc());
                }
            }

            @Override
            public void onError(Throwable e) {
                toastShortShow("图片上传失败");
            }
        };

        HashMap<String, Object> fileMap = new HashMap<>();
        fileMap.put("fileType", "jpg");
        fileMap.put("uploadType", "2");
        fileMap.put("fileContent", strByBase64);
        fileMap.put("operatorId", "");
        ImageUploadRequest uploadService = new ImageUploadRequest();
        uploadService.imgUpload(new ProgressSubscriber(nextListener, mContext), fileMap);
    }

    @Override
    public void onError(Throwable e) {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {

            if (fl_bg.getVisibility() == View.VISIBLE) {

                ivBlackBg.startAnimation(animationOut);
                bigImg.animaTo(mInfo, new Runnable() {
                    @Override
                    public void run() {
                        fl_bg.setVisibility(View.GONE);
                    }
                });
                return false;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
